package com.tmpt.controller.springmvc.handlers;

import com.tmpt.dto.Result;
import com.tmpt.utils.WebUtils;
import com.tmpt.utils.report.DocUtils;
import com.tmptSource.dto.OwCommiteeRecordDto;
import com.tmptSource.enums.OwCitizenSatisfaction;
import com.tmptSource.service.intf.IOwCommiteeRecordService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wxy on 2017/12/7.
 */
@Controller
@RequestMapping("/springmvc/report")
public class ReportAction {

    @Resource
    private IOwCommiteeRecordService owCommiteeRecordService;

    @RequestMapping(value = "/commiteeLogReport")
    @ResponseBody
    public Result commiteeLogReport(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
        System.out.println("=======>Test commiteeLogReport");

        Result<OwCommiteeRecordDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }
            //参数
            String userId = WebUtils.getUserId(session);
            String username = WebUtils.getUsername(session);
            String companyId = WebUtils.getCompanyId(session);
            String commiteeLogId = request.getParameter("commiteeLogId");
            //调用远程服务
            result = owCommiteeRecordService.selectById(Long.parseLong(commiteeLogId));
            OwCommiteeRecordDto owCommiteeRecordDto = result.getResultObj();

            String fileName = owCommiteeRecordDto.getA2userName()+".doc";
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
            OutputStream outputStream = response.getOutputStream();

            DocUtils docUtil=new DocUtils();
            Map<String, Object> dataMap=new HashMap<String, Object>();
            dataMap.put("recorddate", owCommiteeRecordDto.getRecordDate());
            dataMap.put("a2username", owCommiteeRecordDto.getA2userName());
            dataMap.put("address", owCommiteeRecordDto.getAddress());
            dataMap.put("citizenadvise", owCommiteeRecordDto.getCitizenAdvise());
            dataMap.put("content", owCommiteeRecordDto.getContent());
            String satis1 = "";
            String satis2 = "";
            String satis3 = "";
            if(owCommiteeRecordDto.getOwCitizenSatisfaction().equalsIgnoreCase(OwCitizenSatisfaction.GreatSatisfaction.getKeyName())){
                satis1="Y";
            }
            if(owCommiteeRecordDto.getOwCitizenSatisfaction().equalsIgnoreCase(OwCitizenSatisfaction.Satisfaction.getKeyName())){
                satis2="Y";
            }
            if(owCommiteeRecordDto.getOwCitizenSatisfaction().equalsIgnoreCase(OwCitizenSatisfaction.Dissatisfaction.getKeyName())){
                satis3="Y";
            }
            dataMap.put("satis1", satis1);
            dataMap.put("satis2", satis2);
            dataMap.put("satis3", satis3);
            dataMap.put("remark", owCommiteeRecordDto.getRemak());
            docUtil.createDocStream(dataMap, "commitee_work_log.xml", outputStream);

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault("Exception:"+e.getLocalizedMessage());
        }finally {
            System.out.println("final+"+result.getContent());
        }
    }




}
